package salb1;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import common.CommonHelper;


public class Salb1Genotype {
    
    private List<List<Integer>> groups;
    private int numOfStations;
    private int numOfOperations;
    
    public Salb1Genotype(int numOfStations, int numOfOperations) {
        
        this.numOfStations = numOfStations;
        this.numOfOperations = numOfOperations;
        this.groups = new ArrayList<List<Integer>>(this.numOfStations);
        for (int stationNum = 0; stationNum < this.numOfStations; stationNum++) {
            this.groups.add(new LinkedList<Integer>());
        }
    }
    
    public Salb1Genotype(String genotype) {
        
        String[] groupsStr = genotype.split("\\]\\[");
        groupsStr[0] = groupsStr[0].substring(2);
        groupsStr[groupsStr.length-1] = groupsStr[groupsStr.length-1].substring(0, groupsStr[groupsStr.length-1].length()-2);
        
        this.groups = new ArrayList<List<Integer>>();
        for (String groupStr : groupsStr) {
                     
            List<Integer> group = new LinkedList<Integer>();
            if (! groupStr.equals("")) {
                for (String operationNum : groupStr.split(",")) {
                    group.add(new Integer(operationNum));
                }
            }
            this.groups.add(group);
        }    
    }
    
    public Salb1Genotype(Salb1Genotype aGenotype) {
        
        this.numOfStations = aGenotype.numOfStations;
        this.numOfOperations = aGenotype.numOfOperations;
        
        
        this.groups = new LinkedList<List<Integer>>();
        for (List<Integer> group : aGenotype.groups) {
            this.groups.add(new LinkedList<Integer>(group));
        }
    }

    public void setGroups(List<List<Integer>> groups) {
        this.groups = groups;
    }

    public List<List<Integer>> getGroups() {
        return groups;
    }

    public List<Integer> getOperationsForStation(int machineNum) {
        return groups.get(machineNum);
    }

    public int getNumOfOperations() {
        return numOfOperations;
    }
    
    public int getNumOfStations() {
        return numOfStations;
    }
    
    public String toString() {
        
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (List<Integer> group : groups) {
            sb.append("[");
            sb.append(CommonHelper.join(group, ","));
            sb.append("]");
        }
        sb.append("]");
        return sb.toString();
        
    }
    
    @Override
    public boolean equals(Object obj) {
        
        Salb1Genotype other = (Salb1Genotype)obj;
        if (this.groups.size() != other.groups.size()) {
            return false;
        }
        for (List<Integer> group : this.groups) {
            if (! other.groups.contains(group)) {
                return false;
            }
        }
        return true;
    }
}
